using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;
using Telerik.WinControls.UI;
using BO;

namespace AnimalFarm.Product_manage
{
    public partial class ListDairyProduct : Telerik.WinControls.UI.RadForm
    {
        B_DairyProduct B_dairyProduct = new B_DairyProduct();
        B_Farmer B_farmer = new B_Farmer();

        int dairyProductID,farmerID,milkTreatmentID;
        public ListDairyProduct()
        {
            InitializeComponent();
        }

        //load dairy product
        private void loadDairyProduct()
        {
            DataTable aTable = B_dairyProduct.GetAllDairyProduct();
            grvDairyProduct.DataSource = aTable;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void ListDairyProduct_Load(object sender, EventArgs e)
        {
            loadDairyProduct();
            loadContextMenu();
        }

        //load context menu
        private void loadContextMenu()
        {
            ContextMenu menu = new ContextMenu();
            MenuItem item = new MenuItem();
            item.Text = "Delete";
            menu.MenuItems.Add(item);
            grvDairyProduct.ContextMenu = menu;
            item.Click += new EventHandler(item_Click);
        }

        void item_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(this, "Are you sure", "Deleting product", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                int dairyProductID = int.Parse(grvDairyProduct.SelectedRows[0].Cells["DairyProductID"].Value.ToString());
                bool isOk = B_dairyProduct.RemoveOneDairyProduct(dairyProductID);
                if (isOk)
                {
                    MessageBox.Show(this, "Product deleted successful", "Product deleting", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadDairyProduct();
                }
                else
                {
                    MessageBox.Show(this, "Can not delete this product", "Product deleting", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void grvDairyProduct_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvDairyProduct.SelectedRows.Count > 0)
                {
                    GridViewRowInfo row = grvDairyProduct.SelectedRows[0];
                    dairyProductID = int.Parse(row.Cells["DairyProductID"].Value.ToString());
                    string productName = row.Cells["DairyProductName"].Value.ToString();
                    string description = row.Cells["Description"].Value.ToString();
                    farmerID = int.Parse(row.Cells["FarmID"].Value.ToString());
                    milkTreatmentID = int.Parse(row.Cells["MilkTreatmentID"].Value.ToString());

                    Farmer_Info info = B_farmer.GetOneFarmer(farmerID);
                    txtDescription.Text = description;
                    txtOwner.Text = info.FarmerName;
                    txtProductName.Text = productName;
                }
            }
            catch (Exception ex) { }
        }

        private void btnSaveChange_Click(object sender, EventArgs e)
        {
            try
            {
                DairyProduct_Info info = new DairyProduct_Info();
                info.DairyProductID = dairyProductID;
                info.DairyProductName = txtProductName.Text;
                info.Description = txtDescription.Text;
                info.FarmID = farmerID;
                info.MilkTreatmentID = milkTreatmentID;

                bool isOk = B_dairyProduct.EditOneDairyProduct(info);
                if (isOk)
                {
                    MessageBox.Show(this, "Product updated successful", "Product updating", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadDairyProduct();
                }
                else
                {
                    MessageBox.Show(this, "Product updating is fail", "Product updating", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex) { }
        }
    }
}
